package team.se.tms.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.se.tms.dao.impl.DaoImpl;
import team.se.tms.dto.LabelLibDetail;
import team.se.tms.dto.Page;
import team.se.tms.exception.DeleteException;
import team.se.tms.po.LabelLibInfo;
import team.se.tms.po.TeachCourseInfo;
import team.se.tms.service.LabelInfoService;
import team.se.tms.service.LabelLibInfoService;
import team.se.tms.util.KeyUtil;

import java.util.List;

/**
 * 标签库业务层
 *
 * @author johnny
 * @date 2018/11/24
 */
@Service("LabelLibInfoService")
@Transactional(rollbackFor = Exception.class)
public class LabelLibInfoServiceImpl implements LabelLibInfoService{

    @Autowired
    public DaoImpl dao;

    @Autowired
    public LabelInfoService labelInfoService;

    /**
     * 添加标签库信息
     *
     * @param labelLibDetail 标签库信息
     * @return String 页面地址
     * @throws Exception 异常
     * @create on 2018/11/24 by johnny
     */
    @Override
    public Boolean saveLabelLib(LabelLibDetail labelLibDetail) throws Exception {
        //生成主键
        labelLibDetail.setLabelLibId(KeyUtil.generatePrimaryKey());
        //插入标签库信息表
        dao.insert("LabelLibInfoMapper.insertLabelLib",labelLibDetail);
        //插入标签标签库关联表
        dao.insert("LabelLibInfoMapper.insertLabel-LabelLib",labelLibDetail);
        return true;
    }

    /**
     * 根据分页查询标签库信息详情列表
     *
     * @param page 分页
     * @return String 页面地址
     * @throws Exception 异常
     * @create on 2018/11/24 by johnny
     */
    @Override
    public List<LabelLibDetail> listAllLabelLibDetails(Page page) throws Exception {
        return dao.selectList("LabelLibInfoMapper.selectAllLabelLibDetails",page);
    }

    /**
     * 根据分页查询标签库信息列表
     *
     * @param page 分页
     * @return String 页面地址
     * @throws Exception 异常
     * @create on 2018/11/24 by johnny
     */
    @Override
    public List<LabelLibInfo> listAllLabelLibInfos(Page page) throws Exception {
        return dao.selectList("LabelLibInfoMapper.selectAllLabelLibInfos",page);
    }

    /**
     * 修改标签库信息
     *
     * @param labelLibDetail 标签库信息实例
     * @return String 页面地址
     * @throws Exception 异常
     * @create on 2018/12/29 by johnny
     */
    @Override
    public Boolean modifyLabelLib(LabelLibDetail labelLibDetail) throws Exception {
        //更新标签库名
        dao.update("LabelLibInfoMapper.updateLabelLib",labelLibDetail);
        //根据标签库id删除标签标签库关联表中的数据
        dao.delete("LabelLibInfoMapper.deleteLabel-LabelLibByLabelLibId",labelLibDetail.getLabelLibId());
        //插入关联表
        dao.insert("LabelLibInfoMapper.insertLabel-LabelLib",labelLibDetail);
        return true;
    }
    /**
     * 查询被使用的标签
     *
     * @param labelLibId 参数描述
     * @return String 页面地址
     * @throws Exception 异常
     * @create on 2018/11/24 by johnny
     */
    @Override
    public List<String> listUsingLabelIds(String labelLibId) throws Exception {
        return dao.selectList("LabelLibInfoMapper.selectUsingLabelIdsByLabelLibId",labelLibId);
    }


    /**
     * 删除标签库信息
     *
     * @param labelLibId 标签库id
     * @return Boolean 删除结果
     * @throws Exception 异常
     * @create on 2018/11/24 by johnny
     */
    @Override
    public Boolean removeLabelLib(String labelLibId) throws Exception {
        List<TeachCourseInfo> teachCourseInfoList =
            dao.selectList("TeachCourseInfoMapper.selectTeachCourseInfosByLabelLibId",labelLibId);
        //如果有授课使用标签库
        if(teachCourseInfoList.size()>0){
            throw new DeleteException("标签库被使用,无法删除");
        }
        if(dao.delete("LabelLibInfoMapper.deleteLabelLibByLabelLibId",labelLibId)<=0){
            throw new DeleteException();
        }

        dao.delete("LabelLibInfoMapper.deleteLabel-LabelLibByLabelLibId",labelLibId);
        return true;
    }


}
